﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NL.ADA.ConfigService.DAL.EntityClasses;
using NL.ADA.ConfigService.DAL.DatabaseSpecific;
using NL.ADA.ConfigService.DAL.FactoryClasses;
using NL.ADA.ConfigService.DAL.HelperClasses;
using NL.ADA.ConfigService.DAL.RelationClasses;
using NL.ADA.ConfigService.DAL.TypedListClasses;
using NL.ADA.ConfigService.DAL;
using SD.LLBLGen.Pro.ORMSupportClasses;
using NL.ADA.Common;

namespace NL.ADA.ConfigClient
{
    public class ConfigManager : ManagerBase<ConfigEntity>
    {
        //public virtual EntityCollection<ConfigEntity> GetConfigByKey(string key, int appId)
        //{
        //    var result = new EntityCollection<ConfigEntity>();
        //    IRelationPredicateBucket bucket = new RelationPredicateBucket(ConfigFields.Key == key);
        //    bucket.PredicateExpression.AddWithAnd(ConfigFields.ApplicationId == appId);
        //    try
        //    {
        //        using (DataAccessAdapter adapter = new DataAccessAdapter())
        //        {
        //            adapter.FetchEntityCollection(result, bucket);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;

        //}
        //public virtual ConfigEntity GetConfigByKey(string key, int appId, int modeId)
        //{
        //    var result = new ConfigEntity();
        //    IRelationPredicateBucket bucket = new RelationPredicateBucket(ConfigFields.Key == key);
        //    bucket.PredicateExpression.AddWithAnd(ConfigFields.ApplicationId == appId);
        //    bucket.PredicateExpression.AddWithAnd(ConfigFields.ModeId == modeId);
        //    try
        //    {
        //        using (DataAccessAdapter adapter = new DataAccessAdapter())
        //        {
        //            adapter.FetchEntity(result);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;

        //}
        //public virtual string GetConfigValueByKey(string key, int appId, int modeId)
        //{
        //    var result = new ConfigEntity();
        //    IRelationPredicateBucket bucket = new RelationPredicateBucket(ConfigFields.Key == key);
        //    bucket.PredicateExpression.AddWithAnd(ConfigFields.ApplicationId == appId);
        //    bucket.PredicateExpression.AddWithAnd(ConfigFields.ModeId == modeId);
        //    try
        //    {
        //        using (DataAccessAdapter adapter = new DataAccessAdapter())
        //        {
        //            adapter.FetchEntity(result);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result.Value;

        //}

        public CustomConfigList GetAllConfigs(int appId,int modeId)
        {
            var result = new CustomConfigList();
            SelectAllConfigTypedList list = new SelectAllConfigTypedList();
            IPredicateExpression bucket = new PredicateExpression();

            if (appId > 0)
            {
                bucket.Add(ConfigFields.ApplicationId == appId);
            }

            if (modeId > 0)
            {
                bucket.Add(ConfigFields.ModeId == modeId);
            }
            
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchTypedList(list,bucket);
            }

            foreach (SelectAllConfigRow row in list)
            {
                var customConfig = new CustomConfig();
                customConfig.ApplicationId = row.ApplicationId;
                customConfig.ApplicationCode =row.AppCode;
                customConfig.ConfigTypeId = row.ConfigTypeId;
                customConfig.Key = row.Key;
                customConfig.Value = row.Value;
                customConfig.ModeName = row.ModeName;
                customConfig.ModeId = row.ModeId;
                result.Add(customConfig);
            }
            return result;
        }

    }
}
